package com.ruoyi.web.controller.applet;

import com.ruoyi.applet.appletBanner.domain.AppletBanner;
import com.ruoyi.applet.appletBanner.service.IAppletBannerService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.config.ServerConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 小程序轮播图Controller
 * @author ruoyi
 * @date 2024-11-19
 */
@Controller
@RequestMapping("/applet/appletBanner")
public class AppletBannerController extends BaseController {
    
    private final String prefix = "applet/appletBanner";
    
    @Resource
    private IAppletBannerService appletBannerService;
    @Resource
    private ServerConfig serverConfig;
    
    @RequiresPermissions("applet:appletBanner:view")
    @GetMapping()
    public String appletBanner() {
        return prefix + "/appletBanner";
    }
    
    /**
     * 查询小程序轮播图列表
     */
    @RequiresPermissions("applet:appletBanner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppletBanner appletBanner) {
        startPage();
        List<AppletBanner> list = appletBannerService.selectAppletBannerList(appletBanner);
        return getDataTable(list);
    }
    
    /**
     * 导出小程序轮播图列表
     */
    @RequiresPermissions("applet:appletBanner:export")
    @Log(title = "小程序轮播图", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppletBanner appletBanner) {
        List<AppletBanner> list = appletBannerService.selectAppletBannerList(appletBanner);
        ExcelUtil<AppletBanner> util = new ExcelUtil<AppletBanner>(AppletBanner.class);
        return util.exportExcel(list, "小程序轮播图数据");
    }
    
    /**
     * 新增小程序轮播图
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }
    
    /**
     * 新增保存小程序轮播图
     */
    @RequiresPermissions("applet:appletBanner:add")
    @Log(title = "小程序轮播图", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppletBanner appletBanner) {
        return toAjax(appletBannerService.insertAppletBanner(appletBanner));
    }
    
    /**
     * 修改小程序轮播图
     */
    @RequiresPermissions("applet:appletBanner:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        AppletBanner appletBanner = appletBannerService.selectAppletBannerById(id);
        mmap.put("appletBanner", appletBanner);
        return prefix + "/edit";
    }
    
    /**
     * 修改保存小程序轮播图
     */
    @RequiresPermissions("applet:appletBanner:edit")
    @Log(title = "小程序轮播图", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppletBanner appletBanner) {
        return toAjax(appletBannerService.updateAppletBanner(appletBanner));
    }
    
    /**
     * 删除小程序轮播图
     */
    @RequiresPermissions("applet:appletBanner:remove")
    @Log(title = "小程序轮播图", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(appletBannerService.deleteAppletBannerByIds(ids));
    }
    
    /**
     * 通用上传请求（单个）
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(MultipartFile file) {
        try {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath() + "/applet/banner";
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            ajax.put("url", url);
            ajax.put("fileName", fileName);
            ajax.put("newFileName", FileUtils.getName(fileName));
            ajax.put("originalFilename", file.getOriginalFilename());
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
